package com.cf.bank.controller;

import com.cf.bank.pojo.User;
import com.cf.common.ResponseCode;
import com.cf.common.ServerResponse;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenfeng
 * @date 2020/4/5 20:49
 */
@RestController
@RequestMapping("/bank")
public class BankController {

    private Logger logger = Logger.getLogger(BankController.class);

    @RequestMapping(value = "/subLogin")
    public ServerResponse subLogin(User user) {
        ServerResponse res = new ServerResponse();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassword());
        try {
            subject.login(token);
            Thread.sleep(500);
            res.setCode(ResponseCode.SUCCESS.getCode());
            res.setMsg(ResponseCode.SUCCESS.getDesc());
        } catch (AuthenticationException e) {
            logger.info("登录失败，错误信息为：" + e.getMessage());
            res.setCode(ResponseCode.ERROR.getCode());
            res.setMsg(ResponseCode.ERROR.getDesc());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return res;
    }

}
